﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace OA.DAL
{
    public class Authority : Base<Model.Authority>
    {
        public Authority(ISession isession)
            : base(isession)
        {

        }

        public override Model.Authority GetModel(object id)
        {
            Model.Authority ret = null;
            var list = Get("ID=?", null, 0, 0, id);
            if (list.Count > 0)
            {
                ret = list[0];
            }
            return ret;
        }

        public Model.Authority GetModelByWebSite(Common.EnumType.RoleUserType UserType, string RoleUserCode, string WebSiteCode)
        {
            Model.Authority model = null;
            string where = "UserType=? and RoleUserCode=? and WebSiteCode=? and MenuCode is null and ButtonCode is null";
            object[] ps = { UserType, RoleUserCode, WebSiteCode };
            var list = Get(where, null, 0, 0, ps);
            if (list.Count > 0)
            {
                model = list[0];
            }
            return model;
        }

        public Model.Authority GetModelByMenu(Common.EnumType.RoleUserType UserType, string RoleUserCode, string WebSiteCode, string MenuCode)
        {
            Model.Authority model = null;
            string where = "UserType=? and RoleUserCode=? and WebSiteCode=? and MenuCode=? and ButtonCode is null";
            object[] ps = { UserType, RoleUserCode, WebSiteCode, MenuCode };
            var list = Get(where, null, 0, 0, ps);
            if (list.Count > 0)
            {
                model = list[0];
            }
            return model;
        }

        public Model.Authority GetModelByButton(Common.EnumType.RoleUserType UserType, string RoleUserCode, string WebSiteCode, string MenuCode, string ButtonCode)
        {
            Model.Authority model = null;
            string where = "UserType=? and RoleUserCode=? and WebSiteCode=? and MenuCode=? and ButtonCode=?";
            object[] ps = { UserType, RoleUserCode, WebSiteCode, MenuCode, ButtonCode };
            var list = Get(where, null, 0, 0, ps);
            if (list.Count > 0)
            {
                model = list[0];
            }
            return model;
        }

        #region 验证权限


        /// <summary>
        /// 验证权限
        /// </summary>
        /// <param name="webSiteCode">站点编号</param>
        /// <param name="menuCode">菜单编号，不为空则验证菜单权限</param>
        /// <param name="buttonCode">按钮编号，不为空则验证按钮权限</param>
        /// <param name="userName">用户账号</param>
        public bool ValidateAuthority(string webSiteCode, string menuCode, string buttonCode, string userName)
        {
            if (!string.IsNullOrEmpty(buttonCode))
            {
                return ValidateButtonAuthority(webSiteCode, menuCode, buttonCode, userName);
            }
            else if (!string.IsNullOrEmpty(menuCode))
            {
                return ValidateMenuAuthority(webSiteCode, menuCode, userName);
            }
            else
            {
                return ValidateWebSiteAuthority(webSiteCode, userName);
            }
        }

        /// <summary>
        /// 验证按钮权限
        /// </summary>
        public bool ValidateButtonAuthority(string webSiteCode, string menuCode, string buttonCode, string userName)
        {
            var query = isession.GetNamedQuery("ValidateButtonAuthority");
            query.SetString("UserName", userName);
            query.SetString("WebSiteCode", webSiteCode);
            query.SetString("MenuCode", menuCode);
            query.SetString("ButtonCode", buttonCode);
            return query.UniqueResult<long>() > 0;
        }

        /// <summary>
        /// 验证菜单权限
        /// </summary>
        public bool ValidateMenuAuthority(string webSiteCode, string menuCode, string userName)
        {
            var query = isession.GetNamedQuery("ValidateMenuAuthority");
            query.SetString("UserName", userName);
            query.SetString("WebSiteCode", webSiteCode);
            query.SetString("MenuCode", menuCode);
            return query.UniqueResult<long>() > 0;
        }

        /// <summary>
        /// 验证菜单权限
        /// </summary>
        public bool ValidateWebSiteAuthority(string webSiteCode, string userName)
        {
            var query = isession.GetNamedQuery("ValidateWebSiteAuthority");
            query.SetString("UserName", userName);
            query.SetString("WebSiteCode", webSiteCode);
            return query.UniqueResult<long>() > 0;
        }

        /// <summary>
        /// 验证授权权限
        /// </summary>
        /// <param name="webSiteCode">站点编号</param>
        /// <param name="menuCode">菜单编号，不为空则验证菜单权限</param>
        /// <param name="buttonCode">按钮编号，不为空则验证按钮权限</param>
        /// <param name="userName">用户账号</param>
        public bool ValidateDelegate(string webSiteCode, string menuCode, string buttonCode, string userName)
        {
            if (!string.IsNullOrEmpty(buttonCode))
            {
                return ValidateButtonDelegate(webSiteCode, menuCode, buttonCode, userName);
            }
            else if (!string.IsNullOrEmpty(menuCode))
            {
                return ValidateMenuDelegate(webSiteCode, menuCode, userName);
            }
            else
            {
                return ValidateWebSiteDelegate(webSiteCode, userName);
            }
        }

        /// <summary>
        /// 验证按钮授权权限
        /// </summary>
        public bool ValidateButtonDelegate(string webSiteCode, string menuCode, string buttonCode, string userName)
        {
            var query = isession.GetNamedQuery("ValidateButtonDelegate");
            query.SetString("UserName", userName);
            query.SetString("WebSiteCode", webSiteCode);
            query.SetString("MenuCode", menuCode);
            query.SetString("ButtonCode", buttonCode);
            return query.UniqueResult<long>() > 0;
        }

        /// <summary>
        /// 验证菜单授权权限
        /// </summary>
        public bool ValidateMenuDelegate(string webSiteCode, string menuCode, string userName)
        {
            var query = isession.GetNamedQuery("ValidateMenuDelegate");
            query.SetString("UserName", userName);
            query.SetString("WebSiteCode", webSiteCode);
            query.SetString("MenuCode", menuCode);
            return query.UniqueResult<long>() > 0;
        }

        /// <summary>
        /// 验证菜单授权权限
        /// </summary>
        public bool ValidateWebSiteDelegate(string webSiteCode, string userName)
        {
            var query = isession.GetNamedQuery("ValidateWebSiteDelegate");
            query.SetString("UserName", userName);
            query.SetString("WebSiteCode", webSiteCode);
            return query.UniqueResult<long>() > 0;
        }
        #endregion
    }
}
